<template>
  <view class="news-container">
    <!-- 顶部标题栏 -->
    <view class="header">
      <view class="title">景区动态</view>
      <view class="subtitle">及时了解景区最新资讯</view>
    </view>

    <!-- 动态列表 -->
    <view class="news-list">
      <view v-for="item in newsList" 
            :key="item.id" 
            class="news-item"
            @click="showNewsDetail(item)">
        <!-- 左侧时间 -->
        <view class="time-box">
          <text class="date">{{ formatDate(item.createTime) }}</text>
          <text class="time">{{ formatTime(item.createTime) }}</text>
        </view>
        
        <!-- 右侧内容 -->
        <view class="content-box">
          <view class="news-title">{{ item.title }}</view>
          <view class="news-brief">{{ item.brief }}</view>
          <!-- 如果有图片则显示 -->
          <image v-if="item.imageUrl" 
                 :src="item.imageUrl" 
                 mode="aspectFill" 
                 class="news-image"></image>
          <view class="news-footer">
            <text class="source">{{ item.source }}</text>
            <text class="read-count">{{ item.readCount }}阅读</text>
          </view>
        </view>
      </view>
    </view>

    <!-- 加载更多 -->
    <view class="loading" v-if="loading">
      <uni-icons type="spinner-cycle" size="20"></uni-icons>
      <text>加载中...</text>
    </view>
  </view>
</template>

<script>
import config from '@/config.js'

export default {
  data() {
    return {
      newsList: [],
      page: 1,
      pageSize: 10,
      loading: false,
      hasMore: true
    }
  },
  onLoad() {
    this.loadNews()
  },
  // 下拉刷新
  onPullDownRefresh() {
    this.page = 1
    this.hasMore = true
    this.loadNews(true)
  },
  // 上拉加载更多
  onReachBottom() {
    if (this.hasMore && !this.loading) {
      this.page++
      this.loadNews()
    }
  },
  methods: {
    async loadNews(isPullDown = false) {
      if (this.loading) return
      this.loading = true
      
      try {
        const res = await uni.request({
          url: '/api/news/list',
          method: 'GET',
          data: {
            page: this.page,
            pageSize: this.pageSize
          }
        })

        if (res.data.success) {
          const processedList = res.data.data.map(item => ({
            ...item,
            imageUrl: item.imageUrl.startsWith('http') || item.imageUrl.startsWith('//')
              ? item.imageUrl 
              : config.baseURL + (item.imageUrl.startsWith('/') ? item.imageUrl : '/' + item.imageUrl)
          }));
          
          console.log('处理后的数据:', processedList);
          
          if (isPullDown) {
            this.newsList = processedList
          } else {
            this.newsList = [...this.newsList, ...processedList]
          }
          this.hasMore = processedList.length === this.pageSize
        } else {
          uni.showToast({
            title: res.data.message || '加载失败',
            icon: 'none'
          })
        }
      } catch (e) {
        console.error('加载新闻列表错误:', e);
        uni.showToast({
          title: '网络错误',
          icon: 'none'
        })
      } finally {
        this.loading = false
        if (isPullDown) {
          uni.stopPullDownRefresh()
        }
      }
    },
    formatDate(dateStr) {
      const date = new Date(dateStr)
      return `${date.getMonth() + 1}.${date.getDate()}`
    },
    formatTime(dateStr) {
      const date = new Date(dateStr)
      return `${date.getHours()}:${date.getMinutes().toString().padStart(2, '0')}`
    },
    showNewsDetail(item) {
      uni.navigateTo({
        url: `/pages/news/detail?id=${item.id}`
      })
    }
  }
}
</script>

<style scoped>
.news-container {
  padding: 20px;
  background: #f5f5f5;
  min-height: 100vh;
}

.header {
  margin-bottom: 30px;
}

.title {
  font-size: 28px;
  font-weight: bold;
  color: #333;
}

.subtitle {
  font-size: 14px;
  color: #999;
  margin-top: 5px;
}

.news-list {
  background: #fff;
  border-radius: 15px;
  padding: 15px;
}

.news-item {
  display: flex;
  padding: 20px 0;
  border-bottom: 1px solid #f0f0f0;
}

.news-item:last-child {
  border-bottom: none;
}

.time-box {
  width: 60px;
  text-align: center;
  margin-right: 15px;
}

.date {
  font-size: 18px;
  color: #333;
  font-weight: 500;
}

.time {
  font-size: 12px;
  color: #999;
  margin-top: 5px;
}

.content-box {
  flex: 1;
}

.news-title {
  font-size: 16px;
  font-weight: 500;
  color: #333;
  line-height: 1.4;
}

.news-brief {
  font-size: 14px;
  color: #666;
  margin-top: 8px;
  line-height: 1.6;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}

.news-image {
  width: 100%;
  height: 180px;
  border-radius: 8px;
  margin-top: 10px;
  background-color: #f5f5f5;
}

.news-footer {
  display: flex;
  justify-content: space-between;
  margin-top: 10px;
  font-size: 12px;
  color: #999;
}

.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px 0;
  color: #999;
}

.loading text {
  margin-left: 8px;
}
</style>
